﻿@model Cxuu.Models.AdminUserDeptCateRole
@{
    ViewData["Title"] = "保存";
}
<div class="content-top-nav">
    <div class="layui-row">
        <div class="layui-col-md11">
            <h3 style="color:#444">
                <i class="layui-icon layui-icon-home"></i>
                部门网站栏目资源管理
            </h3>
        </div>
        <div class="layui-col-md1" style="text-align:right;">
            <button class="layui-btn layui-btn-sm" onclick="loadMain('/Admin/UserDept/Index')">返回列表</button>
        </div>
    </div>
    <hr>
</div>

<div class="layui-fluid" style="margin-bottom:50px;">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <blockquote class="layui-elem-quote">
                        当前用户部门：<b style="color: red;">
                            @ViewBag.info.DeptName
                        </b> 
                    </blockquote>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-form">

            <fieldset class="layui-elem-field">
                <legend><label asp-for="ContentCateRole"></label></legend>
                <div class="layui-field-box">
                    <div class="layui-form-item">
                        <div class="layui-input-inline">
                        <div id="treeContentCateRole" class="cxuu-tree-channal" data-id="0"></div>
                        </div>
                    </div>
                </div>
            </fieldset>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <input asp-for="Id" value="@ViewBag.info.Id" class="layui-input" type="hidden">
                    <button class="layui-btn" lay-submit lay-filter="saveBtn" id="saveBtn"><i class="layui-icon">&#xe605;</i>@ViewData["Title"]</button>
                </div>
            </div>

    </div>
</div>
<script>
    layui.define(function () {
        var form = layui.form, layer = layui.layer, $ = layui.jquery, tree = layui.tree;

        $.getJSON('/Admin/ContentCate/GetTree', function (res) { //?role=1  时，带权限判断的栏目组
            tree.render({
                elem: '#treeContentCateRole',
                data: res,
                id: "treeID",
                showCheckbox: true,
            });
            var s = "@ViewBag.ContentCateRole";
            tree.setChecked('treeID', s.split(","));
        })
        form.render();

        var getCheckedId = function (jsonObj) {
            var id = '';
            $.each(jsonObj, function (index, item) {
                if (id != "") {
                    id = id + "," + item.id;
                }
                else {
                    id = item.id;
                }
                var i = getCheckedId(item.children);
                if (i != "") {
                    id = id + "," + i;
                }

            });
            // var stringResult = id.split(',');
            return id;
        }

        $('#saveBtn').click(function () {
            let checkData = getCheckedId(tree.getChecked('treeID')).split(',');
            let postData = {
                ContentCateRole: JSON.stringify(checkData),
                Id: @ViewBag.info.Id
            };
            $.post('/Admin/UserDept/ContentCateEdit', postData, function (res) {
                if (res.status == 1) {
                    $("#saveBtn").hide();
                    layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                        loadMain('/Admin/UserDept/Index/');
                    });
                } else {
                    layer.msg(res.msg, { icon: 2, anim: 6, time: 5000 });
                }
            });
            return false;
        });

    });

</script>